package com.jasic.util;

import com.jasic.common.GlobalConstant;
import com.jasic.comnt.protocol.bean.LoginResp;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Jasic
 * Date: 12-12-24
 */
public class ProtocolUtil {

    /**
     * 验证用户名密码格式
     * @param userName
     * @param password
     * @return
     */
    public static boolean validateFormat(String userName, String password) {
        return !(userName == null
                || userName.length() > GlobalConstant.LOGIN_USERNAME_LENGTH
                || userName.length() <= 0
                || null == password
                || password.length() != GlobalConstant.LOGIN_PASSWORD_LENGTH);
    }


    /**
     * 包头和包尾：采用 0x7e 表示，
     * 若校验码、消息头以及消息体中出现 0x7e，则要进行转义处理。转义处理定义如下：
     * 　　0x7e ←→ 0x7d 后紧跟一个 0x02；
     * 　　0x7d ←→ 0x7d 后紧跟一个 0x01。
     *
     * @param actulData
     *
     * @return
     */
    public static byte[] wrapRequestData(byte[] actulData) {

        Asserter.notNull(actulData, "The actually byte data which will be wrapped can't be null,please check it!");

        // It's sure that bytes‘s length is samller than actulData's length
        byte[] bytes = new byte[actulData.length * 2];
        int add_count = 0;
        for (int index = 0; index < actulData.length; index++) {
            byte b = actulData[index];

            if (b == GlobalConstant.ADD_CONNTDN_0x7e) {
                bytes[index + add_count] = GlobalConstant.ADD_CONNTDN_0x7d;
                add_count++;
                bytes[index + add_count] = GlobalConstant.ADD_CONNTDN_0x7e_0x02;

            } else if (b == GlobalConstant.ADD_CONNTDN_0x7d) {

                bytes[index + add_count] = GlobalConstant.ADD_CONNTDN_0x7d;
                add_count++;
                bytes[index + add_count] = GlobalConstant.ADD_CONNTDN_0x7d_0x01;

            } else {
                bytes[index + add_count] = b;
            }
        }
        byte[] data = new byte[actulData.length + add_count];
        System.arraycopy(bytes, 0, data, 0, data.length);

        return data;
    }


    /**
     * @param wrappedData
     *
     * @return
     */
    public static byte[] unWrapRequestData(byte[] wrappedData) {

        Asserter.notNull(wrappedData, "The wrappedData byte data which will be wrapped can't be null,please check it!");

        List<Byte> byteList = new ArrayList<Byte>();
        // It's sure that bytes‘s length is samller than wrappedData's length
        for (int index = 0; index < wrappedData.length; index++) {
            byte b = wrappedData[index];

            if (b == GlobalConstant.ADD_CONNTDN_0x7d) {
                if (index + 1 < wrappedData.length) {
                    byte temp = wrappedData[index + 1];
                    if (temp == GlobalConstant.ADD_CONNTDN_0x7d_0x01) {
                        byteList.add(GlobalConstant.ADD_CONNTDN_0x7d);
                        index++;
                    } else if (temp == GlobalConstant.ADD_CONNTDN_0x7e_0x02) {
                        byteList.add(GlobalConstant.ADD_CONNTDN_0x7e);
                        index++;
                    }
                }
            } else {
                byteList.add(b);
            }
        }

        byte[] data = new byte[byteList.size()];
        for (int i = 0; i < byteList.size(); i++) {
            data[i] = byteList.get(i);
        }
        return data;
    }

    /**
     * 登录是否成功
     * @param resp
     * @return
     */
    public static boolean loginSuccess(LoginResp resp){
        return resp.getResponseCode() == GlobalConstant.LOGINRESP_CODE_SUCCESS;
    }
}
